var express = require("express");
var router = express.Router();
const multiparty = require("multiparty")
var {carjModel} = require("../module/fxd_db");
/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});
// 车检验的信息
router.post("/carJ", async (req, res) => {
  let body = req.body;
  await carjModel.create(body);
  res.send({
    code: 200,
  });
});


router.get("/title", async (req, res) => {
  let data = await carjModel.find();
  res.send({
    code: 200,
    data,
  });
});

router.post("/titlegai/:id", async (req, res) => {
  let id = req.params.id;
  // let body = req.body;
  await carjModel.updateMany({ _id: id }, { ststes: false });
  res.send({
    code: 200,
  });
});
// 申请免检车标的图片上传
// router.post('/upload',(req,res)=>{
//   let form = new multiparty.Form()
//   form.uploadDir = "upload"
//   form.parse(req,(err,formData,imgData)=>{
//     res.send({
//       code:200,
//       path:"http://localhost:/fxd/3050/" + imgData.file[0].path
//     })
//   })
// })
router.post('/upload', (req, res) => {
  let form = new multiparty.Form();
  form.uploadDir = "upload";
  form.parse(req, (err, fields, files) => {
    if (err) {
      console.log(err);
      res.send({ code: 500, message: '上传失败' });
      return;
    }

    const imgData = files && files.image && files.image[0]; // 检查字段是否存在
    console.log(files);
    console.log(imgData);
    if (!imgData) {
      res.send({ code: 400, message: '未找到名为 "image" 的文件字段' });
      return;
    }

    res.send({
      code: 200,
      // path: "http://localhost:3050/"+ imgData.path,
      path: "http://localhost:3050/upload/" + imgData.path.replace("upload\\", ""),
    });
  });
});
module.exports = router;
